/**
* CodeDropz Uploader v1.0.10
* Copyright 2019 Glen Mongaya
* CodeDropz - Drag & Drop Uploader
* @version 1.1.0
* @author CodeDropz, Glen Don L. Mongaya
* @license The MIT License (MIT)
*/
!function(e){e.fn.CodeDropz_Uploader_WC=function(a){var r=[],s=[];let o=document.querySelector("form.cart");if(o){let n=new FormData;n.append("action","wc_upload_nonce"),n.append("nonce",dnd_wc_uploader.nonce),fetch(dnd_wc_uploader.ajax_url,{method:"POST",body:n}).then(e=>e.json()).then(({data:e,success:a})=>a&&(dnd_wc_uploader.nonce=e)).catch(console.error)}this.each(function(){var o=e(this),n=e.extend({is_pro:!1,handler:o,form:o.parents("form"),color:"#000",background:"",upload_dir:null,server_max_error:"Uploaded file exceeds the maximum upload size of your server.",max_file:o.data("max")?o.data("max"):5,max_upload_size:o.data("limit")?o.data("limit"):"10485760",supported_type:o.data("type")?o.data("type"):"jpg|jpeg|JPG|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv|xls",max_total_size:"100MB",parallel_uploads:2,chunks:!1,chunk_size:1e4,text:"Drag & Drop Files Here",separator:"or",button_text:"Browse Files",err_message:{maxNumFiles:"You have reached the maximum number of files ( Only %s files allowed )",maxUploadLimit:"Note : Some of the files could not be uploaded ( Only %s files allowed )",maxTotalSize:"The total file(s) size exceeding the max size limit of %s."},on_success:"",in_progress:"",completed:""},a),t={parallelUploads:n.parallel_uploads,chunking:n.chunks,chunkSize:n.chunk_size,progress__id:"codedropz--results"},i=1048576*parseInt(n.max_total_size.replace("[^0-9]/g","")),d=o.data("name"),l='
0 '+dnd_wc_uploader.uploader_text.of+" "+parseInt(n.max_file)+" ";n.handler.wrapAll('');var p=n.form,u=n.handler.parents(".codedropz-upload-wrapper");n.handler.after(l);var c={init:function(){var a=this;s[d=this.getFieldName(d)]=[],r[d]={total:0,uploaded:0,uploading:!0,maxTotalSize:0,maxSize:n.max_upload_size,maxFile:n.max_file},e(".codedropz-upload-handler",u).on("drag dragstart dragend dragover dragenter dragleave drop",function(e){e.preventDefault(),e.stopPropagation()}),e(".codedropz-upload-handler",u).on("dragover dragenter",function(a){e(this).addClass("codedropz-dragover")}),e(".codedropz-upload-handler",u).on("dragleave dragend drop",function(a){e(this).removeClass("codedropz-dragover")}),!e("."+t.progress__id,u).length>0&&u.append(''),this.getUploadFiles(),e(document).on("click","a.remove-file",function(r){e(this).hasClass("deleting")||a.deleteFiles(e(this).data("index"),e(this),e(this).data("name"))})},getFieldName:function(e){return e.replace(/[^a-zA-Z0-9_-]/g,"")},deleteFiles:function(a,r,o){var t,i=s[o],d=this;r.addClass("deleting").text(dnd_wc_uploader.uploader_text.delete);for(var l=0;l0?(a.deleted=!0,r[o].total--,r[o].maxTotalSize=r[o].maxTotalSize-a.size,a.complete&&r[o].uploaded>0&&r[o].uploaded--,progressBar.remove(),e('input[data-index="'+a.progressbar+'"]').remove(),this.resetQueue(o)):console.log("Progress Bar not exists!"),r[o].uploadedi&&!0==n.is_pro&&(u=t,this.validateFiles.setError(n.err_message.maxTotalSize,!1,n.max_total_size)),e(".dnd-upload-counter span",e('input[data-name="'+o+'"]').parents(".codedropz-upload-wrapper")).text(r[o].total))},getUploadFiles:function(){var a=this;e(".codedropz-upload-handler",u).on("drop",function(e){a.handleFiles(e.originalEvent.dataTransfer.files)}),e("a.cd-upload-btn",u).on("click",function(e){e.preventDefault(),n.handler.val(null),n.handler.click()}),n.handler.on("change",function(e){a.handleFiles(this.files)})},handleFiles:function(a){var o=a.length,t=[];if(r[d].maxFile){var l=r[d].maxFile-r[d].uploaded;l>=0&&a.length>l&&(o=l),0==r[d].uploaded&&r[d].total>0&&(o=r[d].maxFile-r[d].total)}if(e("span.has-error-msg",u).remove(),r[d].total>=r[d].maxFile)return this.validateFiles.setError(n.err_message.maxNumFiles,!0,n.max_file);if(o>0)for(var p=0;p0&&this.validateFiles.setError(n.err_message.maxUploadLimit,!1,n.max_file),r[d].total++,r[d].maxTotalSize+=t.size,t.progressbar=this.progressBar.make(t),!1===this.validateFiles.check(t,d)&&(t.error=!0),r[d].maxTotalSize>i&&!0==n.is_pro&&(this.validateFiles.setError(n.err_message.maxTotalSize,!0,n.max_total_size),t.pause=!0),s[d].push(t);r[d].uploading=!0,this.processQueue(s[d],d)},validateFiles:{setError:function(a,r,s){if(e("span.has-error-msg",u).remove(),e("."+t.progress__id,u).after(''+a.replace("%s",s)+""),r)return!1},check:function(a,s){if(!a)return!0;if(a.progressbar){var o=e("#"+a.progressbar).find(".dnd-upload-details");if(e("#"+a.progressbar).find(".has-error").remove(),a.size>r[s].maxSize)return o.append(''+dnd_wc_uploader.drag_n_drop_upload.large_file+""),!1;if(!(regex_type=RegExp("(.*?).("+n.supported_type+")$")).test(a.name.toLowerCase()))return o.append(''+dnd_wc_uploader.drag_n_drop_upload.inavalid_type+""),!1}return a}},resetQueue:function(e){var a=[];if(r[e].uploading=!0,s[e].length>0)for(var o in s[e])!0==!s[e][o].deleted&&a.push(s[e][o]),!0==s[e][o].pause&&r[e].maxTotalSize0&&(s[e]=a),this.processQueue(s[e],e),console.log(s[e]),console.log(r[e]),console.log(c.bytesToSize(r[e].maxTotalSize)+" of "+n.max_total_size)},processQueue:function(a,o){var i=0,d=[];if(r[o].uploading){for(var l in a)!1==a[l].complete&&!1==a[l].error&&!1==a[l].pause&&d.push(a[l]);e.isFunction(n.in_progress)&&n.in_progress.call(this,p,d,s[o]);for(var l=0;l=t.parallelUploads&&!0==n.is_pro))return;0==i&&(r[o].uploading=!1,e.isFunction(n.completed)&&n.completed.call(this,p,o,s[o]))}},progressBar:{make:function(a){var r="dnd-file-"+Math.random().toString(36).substr(2,9),s='
'+a.name+" ("+c.bytesToSize(a.size)+') ';return e("."+t.progress__id,u).append(''+s+"
"),r},setProgress:function(a,r){var s=e(".dnd-progress-bar",e("#"+a));return s.length>0&&(progress_width=r*s.width()/100,e("span",s).addClass("in-progress").animate({width:progress_width},10).text(r+"% "),100==r&&e("span",s).addClass("complete").removeClass("in-progress")),!1}},uploadFile:function(a,s,o){var i=this,d=new FormData,l=1024*t.chunkSize;d.append("supported_type",n.supported_type),d.append("size_limit",n.max_upload_size),d.append("action","dnd_codedropz_upload_wc"),d.append("security",dnd_wc_uploader.nonce),t.chunking&&s.size>l&&!0==n.is_pro?(s.queued=!0,s.chunkSize=l,s.totalChunks=Math.ceil(s.size/s.chunkSize),s.currentChunk=0,this.uploadChunks(a,s,o)):(s.queued=!0,d.append("dnd-wc-upload-file",s.file),s.transfer=e.ajax({url:n.ajax_url,type:p.attr("method"),data:d,dataType:"json",cache:!1,contentType:!1,processData:!1,xhr:function(){var e=new window.XMLHttpRequest;return e.upload.addEventListener("progress",function(e){if(e.lengthComputable){var a=parseInt(100*(e.loaded/e.total));i.progressBar.setProgress(s.progressbar,a)}},!1),e},complete:function(){i.progressBar.setProgress(s.progressbar,100)},success:function(t){t.success?(s.complete=!0,r[o].uploaded++,i.processQueue(a,o),e.isFunction(n.on_success)&&n.on_success.call(this,s.progressbar,t,o,r[o])):(e("#"+s.progressbar).find(".dnd-upload-details").append(''+t.data+""),s.error=!0,i.processQueue(a,o))},error:function(r,n,t){e("#"+s.progressbar).find(".dnd-upload-details").append(''+t+""),s.error=!0,i.processQueue(a,o)}}))},uploadChunks:function(a,s,t){var i=s.chunkSize*s.currentChunk,d=i+s.chunkSize,l=this;d>s.size&&(d=s.size);var u=this.sliceFile(s.file,i,d),c=new FormData;c.append("security",dnd_wc_uploader.nonce),c.append("start",i),c.append("end",d),c.append("total_chunks",s.totalChunks),c.append("chunk_size",s.chunkSize),c.append("chunk",s.currentChunk),c.append("chunks-file",u,s.file.name),c.append("post_id",o.data("id")),c.append("action","dnd_codedropz_upload_chunks_wc"),s.chunkTransfer=e.ajax({url:n.ajax_url,type:p.attr("method"),dataType:"json",data:c,type:"POST",contentType:!1,processData:!1,cache:!1,success:function(o,i,d){s.currentChunk++,chunks_percentage=Math.ceil(s.currentChunk/s.totalChunks*100),l.progressBar.setProgress(s.progressbar,chunks_percentage),s.currentChunk'+n+""),s.error=!0,l.processQueue(a,t)}})},sliceFile:function(e,a,r){return(e.mozSlice?e.mozSlice:e.webkitSlice?e.webkitSlice:e.slice?e.slice:{}).bind(e)(a,r)},bytesToSize:function(e){return 0===e?"0":fileSize=(kBytes=e/1024)>=1024?(kBytes/1024).toFixed(2)+"MB":kBytes.toFixed(2)+"KB"}};c.init()})}}(jQuery);